package com.neuedu.springmvc.controller;


import com.neuedu.springmvc.entity.Role;
import com.neuedu.springmvc.entity.User;
import com.neuedu.springmvc.pojo.MyPojo;
import com.neuedu.springmvc.validation.ValidationGroup1;
import com.neuedu.springmvc.validation.ValidationGroup2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {


    /**
     * http://localhost:8080/springmvc07/save?username=admin&password=123456&age=18
     * @return
     */
    @RequestMapping("/save")
    public String save(ModelMap modelMap , String username, String password, Integer age){


        HttpServletRequest request = null;


        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("age = " + age);


        //request.setAttribute
        modelMap.addAttribute("username",username);
        modelMap.addAttribute("password",password);
        modelMap.addAttribute("age",age);


        return "user/user_result";
        
    }


    /**
     * http://localhost:8080/springmvc07/innerAPI
     * @return
     */
    @RequestMapping("/innerAPI")
    public String innerAPI(Model model, HttpServletRequest request , HttpServletResponse response, HttpSession session){


        System.out.println("model = " + model);
        System.out.println("request = " + request);
        System.out.println("response = " + response);
        System.out.println("session = " + session);

        model.addAttribute("username","使用model接口设置的用户名");
        model.addAttribute("password","使用model接口设置的password");
        model.addAttribute("age","使用model接口设置的age");

        return "user/user_result";

    }








    /**
     * http://localhost:8080/springmvc07/save2?name=admin&pwd=123456&age=18
     *
     * @RequestParam
     *  value/name : 绑定变量重命名（http参数和方法的参数名不一致）
     *  require    : 默认是true， 可以设置为false， 如果为true 则必须传递，否则报错  HTTP状态 400 - 错误的请求 Required String parameter 'name' is not present
     *
     *
     * @return
     */
    @RequestMapping("/save2")
    public String save2(ModelMap modelMap ,
                        @RequestParam(value = "name",required = true) String username,
                        @RequestParam(value = "pwd",required = false)  String password,
                        Integer age){

        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("age = " + age);


        //request.setAttribute
        modelMap.addAttribute("username",username);
        modelMap.addAttribute("password",password);
        modelMap.addAttribute("age",age);

        return "user/user_result";

    }


    /***
     * http://localhost:8080/springmvc07/save3?username=admin&password=123456&realname=管理员
     * @param modelMap
     * @param user
     * @return
     */
    @RequestMapping("/save3")
    public String save3(ModelMap modelMap ,  User user ){

        //交给MyBatis执行插入

        //request.setAttribute
        modelMap.addAttribute("user",user);


        return "user/user_result";

    }



    @RequestMapping("/save4")
    public String save4(ModelMap modelMap , MyPojo pojo ){

        //交给MyBatis执行插入
        //request.setAttribute
        //modelMap.addAttribute("user",user);
        //modelMap.addAttribute("role",role);
        modelMap.addAttribute("pojo",pojo);


        return "user/user_result";

    }



    //delBatch


    @RequestMapping("/delBatch")
    public String delBatch(ModelMap modelMap , Integer[] id ){

        if(id !=null ){
            for (Integer item : id) {
                System.out.println("item = " + item);
            }
        }

        modelMap.addAttribute("id",id);
        return "user/user_result";
    }


    /**
     * 接受批量数据
     * @param modelMap
     * @param pojo
     * @return
     */
    @RequestMapping("/saveUserList")
    public String saveUserList(ModelMap modelMap , MyPojo pojo ){

        System.out.println("批量插入 pojo.getUserList() = " + pojo.getUserList());

        modelMap.addAttribute("pojo",pojo);
        return "user/user_result";
    }




    /**
     * 绑定Map类型参数
     * @param modelMap
     * @param map
     * @return
     */
    @RequestMapping("/saveAccount")
    public String saveAccount(ModelMap modelMap ,@RequestParam Map map ){

        System.out.println("map.getClass() = " + map.getClass());
        System.out.println("map = " + map);

        modelMap.addAttribute("map",map);
        return "user/user_result";
    }


    /**
     * Validated  声明在想要校验单 参数上
     *
     * http://localhost:8080/springmvc07/validation1?username=abcdefqqqqq
     *
     * @param modelMap
     * @param user
     * @return
     */
    @RequestMapping("/validation1")    // username 6-10
    public String validation1(ModelMap modelMap , @Validated(ValidationGroup1.class) User user , BindingResult br){

        //获取所有 校验失败的 消息
        String msg = "";
        if(br.getErrorCount() >0){
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                msg += allError.getDefaultMessage()+"<br>";
            }
        }

        System.out.println("msg = " + msg);

        modelMap.addAttribute("user",user);
        modelMap.addAttribute("msg",msg);
        return "user/user_result";
    }


    @RequestMapping("/validation2")    // realname 3-10
    public String validation2(ModelMap modelMap , @Validated(ValidationGroup2.class) User user , BindingResult br){

        //获取所有 校验失败的 消息
        String msg = "";
        if(br.getErrorCount() >0){
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                msg += allError.getDefaultMessage()+"<br>";
            }
        }

        System.out.println("msg = " + msg);

        modelMap.addAttribute("user",user);
        modelMap.addAttribute("msg",msg);
        return "user/user_result";
    }


    /***
     * 根据条件查询用户表格
     * @param user
     * @return
     */
    static final  List<User> userList = new ArrayList<>();
    static{
        userList.add(new User("admin","123456","超级管理员","13888888888"));
        userList.add(new User("root","123456","超级管理员","13888888888"));
        userList.add(new User("abcd","123456","超级管理员","13888888888"));
        userList.add(new User("qqqq","123456","超级管理员","13888888888"));
        userList.add(new User("username","123456","超级管理员","13888888888"));
    }


    @RequestMapping("/userlist")
    public String  list(
           Model model,
           @ModelAttribute(value="user") User user){


        List list = new ArrayList();

        String username = user.getUsername();
        if(username!=null ){
            //模拟Like查询
            for (User user1 : userList) {
                if(user1.getUsername().contains(username)){
                    list.add(user1);
                }
            }

        }else{
            list.addAll(userList);
        }

        //根据username条件模糊匹配查询用户数据
        //userList
        model.addAttribute("userList",list);

        return "user/user_list";
    }
    

}
